package com.example.viewmodel_livedata.viewModelDemo

import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel

@Composable
fun viewModelScreen() {
    val viewModel: CViewModel = viewModel()
    val count by viewModel.count.observeAsState(initial = 0)
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = count.toString())
        Spacer(modifier = Modifier.height(8.dp))
        Button(
            onClick = {
                viewModel.onCountChanged(count + 2)
            }
        ) {
            Text(text = "add count")
        }
        Spacer(modifier = Modifier.height(8.dp))
        Button(
            onClick = {
                viewModel.onClear(0)
            }
        ) {
            Text(text = "clear to zero")
        }
    }
}